package com.bonc.config;

import com.bonc.utils.Constants;
import com.bonc.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;

/**
 * @author lenovo
 * @Title
 * @Description
 * @date 2021/7/29 10:15
 */
@Configuration
@Slf4j
@RestControllerAdvice
public class ExceptionConfig {

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ResultVO golableException(HttpServletRequest request, HttpServletResponse response, Exception e){
        if (e instanceof SQLException) {
            log.error("数据库异常:{}", e.getMessage());
            return ResultVO.fail(Constants.PageInfo.SYSTEM_ERROR.getCode(), Constants.PageInfo.SYSTEM_ERROR.getMessage());
        }
        return ResultVO.fail(Constants.PageInfo.ELSE_ERROR.getCode(), Constants.PageInfo.ELSE_ERROR.getMessage());
    }
}
